<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class CateController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {


        // select c1.* ,c2.name as parentname,concat(c1.path,',',c1.id) as apath from cate as c1 left join cate as c2 on c1.pid=c2.id order by apath;
        $data=DB::table('cate as c1')
            ->leftJoin('cate as c2','c1.pid','=','c2.id')
            ->select('c1.*','c2.name as parentname',DB::raw("concat(c1.path,',',c1.id) as apath"))
            ->where("c1.name","like","%".$request->input('search')."%" )
            ->orderBy('apath')
            ->paginate($request->input('pages',10));

        foreach($data as $k=> $v)
        {
            $n=substr_count($v->path,',');
            $data[$k]->name=str_repeat('|----',$n).$v->name;
            if($data[$k]->pid==0)
            {
                $data[$k]->parentname='根分类';
            }
        }
        
        return view('admin.cate.index',['data'=>$data,'request'=>$request->all()]);


    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        // select * , concat(path,',',id) as apath from cate order by apath; 
       $data=DB::table('cate')
            ->select( '*',DB::raw("concat(path,',',id) as apath"))
            ->orderBy("apath")
            ->get();
     
       foreach($data as $k =>$v)
       {
        $num=substr_count($v->path,',');
        $data[$k]->name=str_repeat('|----', $num).$v->name;
       }
       return view('admin.cate.create',['data'=>$data]);


    }

   
    public function store(Request $request)
    {
        $data=$request->except('_token');
        if($request->pid==0)
        {
            $data['path']=0;
        }else
        {
        $pidpath=DB::table('cate')->where('id',$request->pid)->first()->path;
        $newpath=$pidpath.','.$request->pid;
        $data['path']=$newpath;
        }

        $res=DB::table('cate')->insert($data);
        if($res)
        {
            return back()->with(['info'=>'添加成功']);
        }else
        {
            return back()->with(['info'=>'添加失败']);
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
       $data=DB::table('cate')->where('id',$id)->first();

       $alldata=DB::table('cate')
            ->select( '*',DB::raw("concat(path,',',id) as apath"))
            ->orderBy("apath")
            ->get();
     
       foreach($alldata as $k =>$v)
       {
        $num=substr_count($v->path,',');
        $alldata[$k]->name=str_repeat('|----', $num).$v->name;
       }



        return  view('admin.cate.edit',['data'=>$data,'alldata'=>$alldata]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
       $data=$request->except('_token','_method');

       $pidpath=DB::table('cate')->where('id',$request->pid)->first()->path;
        $newpath=$pidpath.','.$request->pid;
        $data['path']=$newpath;

        $res=DB::table('cate')->where('id',$id)->update($data);
        if($res)
        {
            return back()->with(['info'=>'编辑成功']);
        }else
        {
            return back()->with(['info'=>'编辑失败']);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
       $data=DB::table('cate')->where('id',$id)->first();
       $all=DB::table('cate')->get();
       foreach ($all as $k => $v) {
           if($v->pid==$id)
            return back()->with(['info'=>'有孩儿']);
       }
       $res=DB::table('cate')->delete($id);
        if($res)
        {
            return back()->with(['info'=>'删除成功']);
        }else
        {
            return back()->with(['info'=>'删除失败']);
        }

    }
}
